package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyWuQiongDa;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

import static com.gitee.apanlh.web.http.HttpContentType.APPLICATION_JSON;

/**
 *  安心-无穷大API20A
 *
 *  @author 李源聪
 */
@ApiPlan(productId = "64", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5WuQiongDa implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(APPLICATION_JSON);
        Map<Object, Object> hashMap = MapUtils.newHashMap(
                newMap -> newMap.put("phoneMD5", apiDataExchangeParameter.getPhoneMd5()));
        return JsonUtils.toJson(hashMap);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getBoolean("data"), true)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(APPLICATION_JSON);
        OrgApplyWuQiongDa data = new OrgApplyWuQiongDa();
        // 用户姓名
        data.setCusName(apiApplyParameter.getUserName());
        // 用户手机号
        data.setPhone(apiApplyParameter.getPhone());
        //  贷款额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            String money = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", "30000")
                    .when(apiApplyParameter.getLoanAmount(), "2", "50000")
                    .when(apiApplyParameter.getLoanAmount(), "3", "100000")
                    .when(apiApplyParameter.getLoanAmount(), "4", "200000")
                    .end();
            data.setAmount(money);
        }
        //  车辆状态
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String money = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无车产")
                    .when(apiApplyParameter.getCarInfo(), "2", "有车不抵押")
                    .when(apiApplyParameter.getCarInfo(), "3", "有车可抵押")
                    .when(apiApplyParameter.getCarInfo(), "4", "有车")
                    .end();
            data.setCarStatus(money);
        }
        // 来源
        data.setLaiyuan("安心分期");
        //  城市名称
        data.setCityName(apiApplyParameter.getCity().replace("市",""));
        // IP
        data.setIp(apiApplyParameter.getIp());
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //填写剩下所有资质
        data.setRemark(apiFieldConverter.getExtendInfo());
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
